from matplotlib import pyplot as plt
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.metrics import roc_curve, auc
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn import svm


# # 需要用到的函数的定义
# # 取出想要的分类器
from sklearn.naive_bayes import GaussianNB


def get_clsFunc(name):
    if name == '1':
        clf = LogisticRegression(solver='liblinear')
        # model=LogisticRegression(solver=’liblinear’)
    elif name == '2':
        clf = svm.SVC(kernel = 'linear', probability = True)#kernel : {'linear', 'poly', 'rbf', 'sigmoid', 'precomputed'}
    elif name == '3':
        clf = LinearDiscriminantAnalysis(solver = 'svd', store_covariance = True)  # solver={'svd', 'lsqr', 'eigen'}
    return clf


# Compute micro-average ROC curve and ROC area
def roc_plot(y_true, y_score):
    fpr, tpr, thresholds = roc_curve(y_true, y_score)
    roc_auc = auc(fpr, tpr)
    print('auc:', roc_auc)
    print('负正率:', fpr)
    print('真正率:', tpr)
    print('阈值:', thresholds)
    plt.plot(fpr, tpr, 'b')
    plt.xlim(([0, 1]))
    plt.ylim(([0, 1]))
    plt.xlabel('fpr')
    plt.ylabel('tpr')
    plt.show()


# 数据可视化
def plot_data(X, y):
    plt.figure(figsize=(10, 8))
    pos = np.where(y == 1)  # 找到y=1的位置
    neg = np.where(y == 0)  # 找到y=0的位置
    p1, = plt.plot(np.ravel(X[pos, 0]), np.ravel(X[pos, 1]), 'ro', markersize=8)
    p2, = plt.plot(np.ravel(X[neg, 0]), np.ravel(X[neg, 1]), 'g^', markersize=8)
    plt.xlabel("X1")
    plt.ylabel("X2")
    plt.legend([p1, p2], ["y=1", "y=0"])
    # plt.show()
    return plt


# 决策边界的可视化
def plot_decisionBoundary(X, y, model):
    plt = plot_data(X, y)
    # 线性边界
    w = model.coef_
    b = model.intercept_
    print("特征系数:", w)
    print('截距:', b)
    xp = np.linspace(np.min(X[:, 0]), np.max(X[:, 0]), 100)
    yp = -(w[0, 0] * xp + b) / w[0, 1]
    plt.plot(xp, yp, 'b-', linewidth=3.0)
    plt.show()
